<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserManager
 * 
 * @author jin
 */

include_once 'BDDManager.inc';
include_once './classes/LogManager.inc';

class UserData {
    
    private $bdd;//type BDDManager
    
    //Constructor
    public function __construct($aBdd){
        $this->bdd = $aBdd;        
    }
    
    //get user
    public function getUser($aPeudo,$aPass){
        $req = "SELECT * FROM gameusers WHERE pseudo = '".$aPeudo."' AND passuser = '".$aPass."'";
        $result = $this->bdd->query($req);
        if(!$result){
            $row = $result->fetch();
            return new GameUser($row[0],$row[1],$row[2]);
        } else {
            $this->bdd->errorMsg();
        }
    }
    
    // create new user
    public function createUser($aPseudo,$aPassuser){
        $req = "INSERT INTO gameusers (pseudo, passuser) 
                    VALUES ('".$aPseudo."','".$aPassuser."')";
        $result = $this->bdd->query($req);
        if (!$result){
            $this->bdd->errorMsg();
        }
    }
    
    //delete an user
    public function deleteUser(GameUser $aUser){
        $req = "DELETE FROM gameuser WHERE iduser =".$aUser->getId();
        if($this->bdd->exec($req)<=0){
            $this->bdd->errorMsg();
        }
    }
    
    ///////////////////////////TO MODIFY/////////////////////////////////////////////
    //user update
    public function setPseudo($aPseudo){
        $this->pseudo = $aPseudo;
        $req = "update gameuser
            set pseudo = '".$aPseudo."'";
        $result = $this->bdd->query($req);
        if(!$result){
            $this->bdd->erroMsg();
        }
    }
    public function setPass($aPassuser){
        $this->passuser = $aPassuser;
        $req = "update gameuser
            set passuser = '".$aPassuser."'";
        $result = $this->bdd->query($req);
        if(!$result){
            echo $this->bdd->erroMsg();
            echo '</br>';
        }
    }
    

    
    public function findUser($aPseudo,$aPassuser){
        $req = "SELECT * FROM gameusers 
            WHERE pseudo = '".$aPseudo."' AND passuser = '".$aPassuser."')"; 
        $result = $this->bdd->query($req);
        if (!$result){
            die($this->bdd->erroMsg());
        } else {
            while($row=mysqli_fetch_row($result)) {
                $this->iduser = $row[0];
                $this->pseudo = $row[1];
                $this->passuser = $row[2];
            }
        }
    }
}

?>
